package com.dys.controller;

import com.dys.pojo.Person;
import com.dys.pojo.Person2;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Arrays;
import java.util.Date;

/**
 * @className: com.dys.controller.PojoTypeParamController
 * @description: SpringMVC_注入POJO类型参数
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/10/26 15:40
 */
@Controller
public class PojoTypeParamController {

    @RequestMapping(value = {"/pojoTypeParam.do"}, method = {RequestMethod.GET})
    public String pojoTypeParam(String pname, String page, String sex, String[] hobby, String birthDate) {
        System.out.println("Spring MVC Pojo 类型参数");
        System.out.println("pname: " + pname);
        System.out.println("page: " + page);
        System.out.println("sex: " + sex);
        System.out.println("hobby: " + Arrays.toString(hobby));
        System.out.println("birthDate: " + birthDate);
        return "pojoTypeParamSuccess";
    }

    /**
     * 使用POJO接收参数时，注意事项：
     * 提交的参数名必须和pojo属性的名称保持一致
     * springmvc底层通过反射给参数列表属性赋值
     * 通过setXXX方法设置属性的值，不是直接通过操作属性or构造器赋值
     * pojo一定要有set方法,不然属性赋值就会失败
     *
     * @param person
     * @return
     */
    @RequestMapping(value = {"/pojoTypeParam2.do"}, method = {RequestMethod.GET})
    public String pojoTypeParam2(Person person) {
        System.out.println("Spring MVC Pojo 类型参数");
        System.out.println(person);
        return "pojoTypeParamSuccess";
    }

    /**
     * 接收日期类型
     *
     * @param birthDate
     * @return
     */
    @RequestMapping(value = {"/pojoTypeParamByDate.do"}, method = {RequestMethod.POST})
    public String pojoTypeParamByDate(@DateTimeFormat(pattern = "yyyy-MM-dd") Date birthDate) {
        System.out.println("Spring MVC 接收日期类型的参数");
        System.out.println(birthDate);
        return "pojoTypeParamSuccess";
    }

    /**
     * 接收pojo的日期类型
     *
     * @param person
     */
    @RequestMapping(value = {"/pojoTypeParamByPojo.do"}, method = {RequestMethod.POST})
    public String pojoTypeParamByPojo(Person2 person) {
        System.out.println("Spring MVC 接收POJO的日期类型参数");
        System.out.println(person);
        return "pojoTypeParamSuccess";
    }
}
